<?php

/**
 * 格子用户中心管理控制器
 *  
 */
class GeziAction extends UserAction {

    public function index() {
        //推荐信息
        $roundDZ = _roundDazhe();
        $this->assign('rounddz', $roundDZ);
        $this->display();
    }

    public function fabu() {
        if (IS_POST) {
            //校验敏感词
            $checkTitle = D('Badword')->checkBadword($_POST['content']);
            if($checkTitle['status'] == false)
                $this->error('标题包含敏感词 -- '.$checkTitle['data']);
            //图片上传
            $savePath           = "/Public/Uploads/" . date("Ymd") . "/";
            $file               = $this->upload($savePath);
            $_POST['s_img']     = $savePath . $file['s_img']['savename'];
            $_POST['b_img']     = $savePath . $file['b_img']['savename'];
            $_POST['pay_score'] = D('UserAccount')->countSum($_POST);
            $fabu               = D('Gezi')->fabu($_POST);
            if ($fabu['status'] == true) {
                $this->success('格子发布成功');
            } else {
                $this->error('格子发布失败' . $fabu['msg']);
            }
            exit;
        } else {
            //推荐信息
            $roundDZ = _roundDazhe();
            $this->assign('rounddz', $roundDZ);
            $this->display();
        }
    }

    /**
     * 次级页面编辑 
     */
    public function linkPage() {
        $this->display('page');
    }

    /**
     * 计算每一项金币数 
     */
    public function countScore() {
        $day  = (int) $_REQUEST['day'];
        $type = t($_REQUEST['type']);
        switch ($type) {
            case 'link':
                $sum  = C('LINK_DAY_PAY') * $day;
                break;
            case 'zoom':
                $sum  = C('ZOOM_PAY_SCORE') * $day;
                break;
            case 'page':
                $sum  = C('PAGE_DAY_PAY') * $day;
                break;
            case 'index_top':
                $sum  = C('INDEX_TOP_PAY') * $day;
                break;
            case 'cat_top':
                $sum  = C('CAT_TOP_PAY') * $day;
                break;
            case 'day_pay':
                $sum  = C('DAY_PAY') * ($day - 1);
                break;
            default :
                $sum  = 0;
                break;
        }
        $data = array(
            'status' => true,
            'msg'    => 'suc',
            'data'   => $sum,
        );
        $this->ajaxReturn($data);
    }

    /**
     * 计算总金币数 
     */
    public function countScoreSum() {
        $total = 0;
        //图片放大
        if ($_REQUEST['zoom'] == 'checked') {
            $total += C('ZOOM_PAY_SCORE');
        }
        //链接dz
        if ($_REQUEST['link_status'] == 'checked') {
            $total += C('LINK_DAY_PAY') * (int) $_REQUEST['link'];
        }
        //次级页面
        if ($_REQUEST['page_status'] == 'checked') {
            $total += C('PAGE_DAY_PAY') * (int) $_REQUEST['page'];
        }
        //发布天数
        if ($_REQUEST['days_status'] == 1) {
            $total += C('DAY_PAY') * ((int) $_REQUEST['days'] - 1);
        }
        //@todo 首页置顶
        //@todo 分类置顶

        $data = array(
            'status' => true,
            'msg'    => 'suc',
            'data'   => $total,
        );
        $this->ajaxReturn($data);
    }
    
    /**
     * 格子列表 
     */
    public function gezilist(){
        $uid = session('uid');
        $model = M('Gezi');
        $type = t($_REQUEST['type']);
        
        $map = array();
        $map['user_id'] = $uid;
        if($type == 'outoftime'){
            $map['end_time'] = array('elt', time());
        }
        
        //取得满足条件的记录数
        $count       = $model
                ->where($map)
                ->count();
        if ($count > 0) {
            import("ORG.Util.Page");

            $limit = 8;
            $order = 'create_time DESC';
            //创建分页对象
            $p     = new Page($count, $limit);
            //分页查询数据

            $voList = $model
                    ->where($map)
                    ->order($order)
                    ->limit($p->firstRow . ',' . $p->listRows)
                    ->select();
            //分页模版
            $TP     = '共有{recordcount}{prev}&nbsp;{liststart}{list}&nbsp;{next}';
            $p->SetPager('default', $TP, array("listlong"      => "6", "first"         => "首页", "last"          => "尾页", "prev"          => "上一页", "next"          => "下一页", "list"          => "*", "disabledclass" => ""));

            //分页显示
            $page = $p->show();
            //模板赋值显示
            $data = array(
                'list'      => $voList,
                'page'      => $page,
                'nowPage'   => $_REQUEST['page'],
                'totalPage' => round($count / $limit),
            );
        }
        
        //推荐信息
        $roundDZ = _roundDazhe();
        $this->assign('rounddz', $roundDZ);
        
        $this->assign('data', $data);
        $this->display();
    }

}

?>
